<?php
include("config.php");
date_default_timezone_set('PRC');

function fixlen($str,$len){
	$i = 0;    
  $tlen = 0;    
  $tstr = '';    
  while ($tlen < $len) {    
      $chr = mb_substr($str, $i, 1, 'utf8');    
      $chrLen = ord($chr) > 127 ? 2 : 1;    
      if ($tlen + $chrLen > $len) break;    
      $tstr .= $chr;    
      $tlen += $chrLen;    
      $i ++;    
  }    
  if ($tstr != $str) {    
      $tstr .= '......';    
  }    
  return htmlspecialchars($tstr,utf-8);
}
		
		
function dirReader($dir){
	$handle=opendir($dir);
	$i=0;
	while($file=readdir($handle))   {
		if   (($file!= ".")and($file!= ".."))   {
		$list[$i]=$file;
		$i=$i+1;
		}
	}
	closedir($handle);  
	return   $list;
}



function checkSql($str){

	//$str=trim($str);
 	//$str=strip_tags($str);
 	//$str=stripslashes($str);
 	//$str=addslashes($str);
 	//$str=rawurldecode($str);
 	//$str=quotemeta($str);
 	//$str=htmlspecialchars($str);
 	$str=preg_replace("/\+|\*|\`|\/|\-|\$|\#|\^|\!|\@|\%|\&|\~|\^|\[|\]|\'|\"|\<|\>|\=|\;/", "", $str);
 	//去除特殊符号+*`/-$#^~!@#$%&[]'"
 	//$str=preg_replace("//s/", "", $str);//去除空格、换行符、制表符
 	return addslashes($str);

}

function cutphoto($o_photo,$d_photo,$width,$height){ 
	$temp_img = imagecreatefromjpeg($o_photo); 
	$o_width  = imagesx($temp_img);                                //取得原图宽 
	$o_height = imagesy($temp_img);                                //取得原图高 	
	//判断处理方法 
	if($width>$o_width || $height>$o_height){        //原图宽或高比规定的尺寸小,进行压缩 	
		$newwidth=$o_width; 
		$newheight=$o_height; 	
		if($o_width>$width){ 
		        $newwidth=$width; 
		        $newheight=$o_height*$width/$o_width; 
		} 	
		if($newheight>$height){ 
		        $newwidth=$newwidth*$height/$newheight; 
		        $newheight=$height; 
		} 	
		//缩略图片 
		$new_img = imagecreatetruecolor($newwidth, $newheight);  
		imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);  
		imagejpeg($new_img , $d_photo);                 
		imagedestroy($new_img); 	
	}else{                                                                                //原图宽与高都比规定尺寸大,进行压缩后裁剪 
	  if($o_height*$width/$o_width>$height){        //先确定width与规定相同,如果height比规定大,则ok 
	          $newwidth=$width; 
	          $newheight=$o_height*$width/$o_width; 
	          $x=0; 
	          $y=($newheight-$height)/2; 
	  }else{                                                                        //否则确定height与规定相同,width自适应 
	          $newwidth=$o_width*$height/$o_height; 
	          $newheight=$height; 
	          $x=($newwidth-$width)/2; 
	          $y=0; 
	  } 	
	  //缩略图片 
	  $new_img = imagecreatetruecolor($newwidth, $newheight);  
	  imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);  
	  imagejpeg($new_img , $d_photo);                 
	  imagedestroy($new_img); 
	  	         
	  $temp_img = imagecreatefromjpeg($d_photo); 
	  $o_width  = imagesx($temp_img);                                //取得缩略图宽 
	  $o_height = imagesy($temp_img);                                //取得缩略图高 
	
	  //裁剪图片 
	  $new_imgx = imagecreatetruecolor($width,$height); 
	  imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height); 
	  imagejpeg($new_imgx , $d_photo); 
	  imagedestroy($new_imgx); 
	} 
}

//登录检查
function checkLogin(){
	if($_SESSION['login_mail']==""){
		echo "<script type=\"text/javascript\">location.href='login.php'</script>";
	}
}

//返回用户头相地址
function showUserAvatar($user){
	$re=mysql_query("select * from `user` where `user_email`='$user'")or die(mysql_error());
	$row=mysql_fetch_array($re);
	$src="avatar.php?id=".$row['id'];
	return $src;
}

//返回图片具体页面图片上传者头相地址	
function showPageAvatar($id){
	$re=mysql_query("select * from `image` where `id`='$id' and `del`='0'")or die(mysql_error());
	$row=mysql_fetch_array($re);
	$src=showUserAvatar($row['post_user']);
	return $src;
}

//按用户email返回用户名
function showUserName($user){
	$re=mysql_query("select * from `user` where `user_email`='$user'")or die(mysql_error());
	$row=mysql_fetch_array($re);
	if($row['user_name']!=""){return $row['user_name'];}
	else return $row['u_id'];
}

//按图片ID返回图片信息
function getImageInfo($id){
	$re=mysql_query("select * from `image` where `id`='$id' and `del`='0'")or die(mysql_error());
	$row=mysql_fetch_object($re);
	$data[]=$row;
	$result=mysql_query("select * from image_update where img_id='$row->id' order by create_time")or die(mysql_error());
	while($r=mysql_fetch_object($result)){
		$data[]=$r;
	}
	return $data;
}

//返回用户所有相册
function showUserGallery($user,$show=-1){
	$re=mysql_query("select * from `gallery` where `user`='$user'")or die(mysql_error());
	echo "<select style='width:200px;height:40px;font-size:20px' name='gallery' class='gallery_select'>";
	while($row=mysql_fetch_array($re)){
		if($show == $row['id']){
			echo "<option value='".$row['id']."' selected='selected'>";
		}else{
			echo "<option value='".$row['id']."'>";
		}
		echo $row['name'];
		echo "</option>";
	}
	if(mysql_num_rows($re) == 0){echo "<option value='0'>无</option>";}
	echo "</select>";
}

//返回用户相册数
function showUserGalleryNum($user){
	$re=mysql_query("select * from `gallery` where `user`='$user'")or die(mysql_error());
	$count=mysql_num_rows($re);
	return $count;
}

function headerLogin(){
	?>
	<div class="search fr"><input class="srch" type="text" id="search" size="20" value="搜索地名" onfocus="this.value=''"/><a id="do_searchs"><img width="12px" height="12px"src="img/search.gif" alt="" /></a></div>				
						<?php if($_SESSION['login_mail']!=""){
							?>
						<div class="user_in">
						<a href="" class=""><img style="margin:3px;float:left" width="25px" height="25px" src="<?php echo showUserAvatar($_SESSION['login_mail']);?>" alt="" /><u><?php echo showUserName($_SESSION['login_mail']);?></u><u class="more"></u></a>
						<ul class="us_in none">
							<li><a href="user_album.php?user=<?php echo $_SESSION['login_mail'];?>">个人主页</a></li>
							<li><a href="edit_user_info.php">设置</a></li>
							<li><a href="login_out.php" class="us_in_li">退出</a></li>
						</ul>
					</div>
					<?php }else{
						echo "<a href='#' class='re_lo login_in' >登录</a>";
						echo "<a href='register.php' class='re_lo'>注册</a>";
						}
}
?>